package com.jiezhuo.controller.jss;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiezhuo.entity.cmeduSchool.XxdwXq;
import com.jiezhuo.entity.jssgl.JssStuJbqk;
import com.jiezhuo.entity.vo.JssTjVo;
import com.jiezhuo.service.IJwXqNjXjService;
import com.jiezhuo.service.IJwXqStudentService;
import com.jiezhuo.service.cmeduSchool.XxdwService;
import com.jiezhuo.service.cmeduSchool.XxdwXqService;
import com.jiezhuo.service.jss.JssStuJbqkService;
import com.jiezhuo.service.jss.JssXtUserlistService;
import com.jiezhuo.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@RequiredArgsConstructor
@RestController
@Api(tags = "寄宿生统计管理")
@Transactional
@RequestMapping("/tjJssCount")
public class TjJssController {

    @Autowired
    private IJwXqNjXjService jwXqNjXjService;

    @Autowired
    IJwXqStudentService jwXqStudentService;

    @Autowired
    JssStuJbqkService jssStuJbqkService;

    @Autowired
    XxdwService xxdwService;

    @Autowired
    XxdwXqService xxdwXqService;

    private final JssXtUserlistService jssXtUserlistService;


    @GetMapping("getLoadXq")
    public R getCountJSSGl() {
        List<XxdwXq> list = xxdwXqService.list();
        if (jssXtUserlistService.isRoleXiao()) {
            String gzXqdm = jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm();
            list = list.stream().filter(x -> x.getNewXqdm().equals(gzXqdm)).collect(Collectors.toList());
        }
        return R.ok().data("data", list);
    }

    @ApiOperation("获取寄宿生统计信息")
    @GetMapping("getCountInfo")
    public R getCountXq(String newXqdm) {
        if (jssXtUserlistService.isRoleXiao()) {
            newXqdm = jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm();
        }
        //所有寄宿生id
        List<String> jssStudentIds = new ArrayList<>();
        Set<String> xqdmSet = new HashSet<>();

        QueryWrapper<JssStuJbqk> jbqkQuery = new QueryWrapper<>();
        jbqkQuery.eq("Removed", "0");
        List<JssStuJbqk> jssstuJbqkList = jssStuJbqkService.list(jbqkQuery);
        jssstuJbqkList.forEach(x -> jssStudentIds.add(x.getStuid()));

        // 获取最大的学期编号
        Map<String, Object> map = new HashMap<>();
        String maxXQ = jwXqNjXjService.getMaxXQ();
        map.put("XQ", maxXQ);
        if (newXqdm != null && !"".equals(newXqdm)) {
            map.put("newXqdm", newXqdm);
        }


        //获取寄宿生管理模块 寄宿生信息


//        // 查询所有寄宿生信息
//
//        // 获取寄宿生总人数
//        List<Map<String, Object>> jssGlCount = jwXqStudentService.getJssGlCount(map);
//        List<JssTjVo> jssTjVosZong = convert(jssGlCount);
//
//        // 统计男性别人数
//        List<Map<String, Object>> jssGlManCount = jwXqStudentService.getJssGlManCount(map);
//        List<JssTjVo> jssTjVosManZong = convert(jssGlManCount);

        // 获取所有寄宿生信息
        List<Map<String, Object>> jssGlAllStudent = jwXqStudentService.getJssGlAllStudent(map);
        if (jssGlAllStudent.size() == 0) {
            return R.ok().data("data", null);
        }
        List<JssTjVo> jssTjVoList = convertIsStu(jssGlAllStudent);
        // 最终的寄宿生数据
        List<JssTjVo> jssTjVosMax = jssTjVoList.stream().filter(x -> jssStudentIds.contains(x.getStuId())).collect(Collectors.toList());

        // 校区分组
        xqdmSet = jssTjVosMax.stream().map(JssTjVo::getNewXqdm).collect(Collectors.toSet());

        List<JssTjVo> list = new ArrayList<>();
        for (String xqdm : xqdmSet) {
            list.addAll(jssTjVoCount(xqdm, jssTjVosMax));
        }

        // 赋值学校和年级
        QueryWrapper<XxdwXq> xxdwXqQueryWrapper = new QueryWrapper<>();
        xxdwXqQueryWrapper.in("NEW_XQDM", xqdmSet);
        List<XxdwXq> xxdwXqList = xxdwXqService.list(xxdwXqQueryWrapper);

        Map<String, String> xxdmxqMap = new HashMap<>();
        for (XxdwXq xxdwXq : xxdwXqList) {
            xxdmxqMap.put(xxdwXq.getNewXqdm(), xxdwXq.getXqjc());
        }

        Map<String, String> dmnjMap = new HashMap<>();

        List<Map<String, Object>> dm_nj = xxdwService.getDistList("dm_nj");
        for (Map<String, Object> dmnj : dm_nj) {
            dmnjMap.put(String.valueOf(dmnj.get("DM")), String.valueOf(dmnj.get("MC")));
        }

        for (JssTjVo jssTjVo : list) {
            jssTjVo.setNjmc(dmnjMap.get(jssTjVo.getNj()));
            jssTjVo.setXqjc(xxdmxqMap.get(jssTjVo.getNewXqdm()));
        }

//        // 获取移除的记录
//        QueryWrapper<JssStuJbqk> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("Removed", 1);

//        List<JssStuJbqk> list = jssStuJbqkService.list(queryWrapper);

        return R.ok().data("data", list);
    }


    private List<JssTjVo> jssTjVoCount(String xqdm, List<JssTjVo> jssTjVoList) {

        jssTjVoList = jssTjVoList.stream().filter(x -> x.getNewXqdm().equals(xqdm)).collect(Collectors.toList());

        Set<String> ngSet = jssTjVoList.stream().map(JssTjVo::getNj).collect(Collectors.toSet());


        List<JssTjVo> jssTjVos = new ArrayList<>();


        // 统计学校
        long countMan = jssTjVoList.stream().filter(x -> x.getXbm().equals("1")).count(); //统计男生
        JssTjVo xx = new JssTjVo();
        xx.setNewXqdm(xqdm);
        xx.setJssNumber(jssTjVoList.size());
        xx.setJssManNuber(Integer.parseInt(countMan + ""));
        jssTjVos.add(xx);

        //统计年级

        for (String ngOne : ngSet) {
            JssTjVo ng = new JssTjVo();
            List<JssTjVo> jssTjVosNg = jssTjVoList.stream().filter(x -> x.getNj().equals(ngOne)).collect(Collectors.toList());
            long jssTjVosManNgCount = jssTjVoList.stream().filter(x -> x.getNj().equals(ngOne) && x.getXbm().equals("1")).count();

            // 设置学校设置年级
            ng.setNj(ngOne);
            ng.setNewXqdm(xqdm);
            ng.setJssNumber(jssTjVosNg.size());
            ng.setJssManNuber(Integer.parseInt(jssTjVosManNgCount + ""));
            jssTjVos.add(ng);

            // 在年级得基础上 设置班级
            Set<String> bjSet = jssTjVosNg.stream().map(JssTjVo::getBj).collect(Collectors.toSet());

            for (String bjOne : bjSet) {
                List<JssTjVo> jssTjVosBj = jssTjVosNg.stream().filter(x -> x.getBj().equals(bjOne)).collect(Collectors.toList());
                long bjcount = jssTjVosNg.stream().filter(x -> x.getBj().equals(bjOne) && x.getXbm().equals("1")).count();


                JssTjVo bj = new JssTjVo();
                bj.setNj(ngOne);
                bj.setBj(bjOne);
                bj.setNewXqdm(xqdm);
                bj.setJssNumber(jssTjVosBj.size());
                bj.setJssManNuber(Integer.parseInt(bjcount + ""));
                jssTjVos.add(bj);

            }
        }
        return jssTjVos;
    }


    private List<JssTjVo> convertIsStu(List<Map<String, Object>> list) {
        List<JssTjVo> jssTjVoList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            JssTjVo jssTjVo = new JssTjVo();
            jssTjVo.setBj(list.get(i).get("BJ").toString());
            jssTjVo.setNewXqdm(list.get(i).get("NEW_XQDM").toString());
            jssTjVo.setNj(list.get(i).get("NJ").toString());
            jssTjVo.setStuId(list.get(i).get("STU_ID").toString());
            jssTjVo.setXbm(list.get(i).get("XBM").toString());

            jssTjVoList.add(jssTjVo);
        }
        return jssTjVoList;
    }
}
